package com.fz.us.admin.base.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具类 - 公用
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-07-13 。
 * ============================================================================
 */
public class CommonUtil {

	/**
	 * 正则匹配
	 *
	 * @param  regex
	 * @param  str
	 *
	 * @return 验证通过返回true
	 */
	private static boolean match(String regex, String str) {
		boolean bool;
		if(StringUtils.isEmpty(regex)) {
			bool = false;
		} else {
			if(StringUtils.isEmpty(str)) {
				bool = false;
			} else {
				bool = Pattern.compile(regex).matcher(str).matches();
			}
		}
		return bool;
	}

	/**
	 * 用户名验证
	 *
	 * @param  username
	 *
	 * @return 验证通过返回true
	 */
	public static boolean isUsername(String username) {
		String regex_username = "^[a-zA-Z]\\w{5,19}$";
		return match(regex_username, username);
	}

    /**
     * 手机号验证
     *
     * @param  mobile
	 *
     * @return 验证通过返回true
     */
    public static boolean isMobile(String mobile) {
		String regex_mobile = "^[1][0,1,2,3,4,5,6,7,8,9][0-9]{9}$";
		return match(regex_mobile, mobile);
    }

	/**
	 * 邮箱验证
	 *
	 * @param  email
	 *
	 * @return 验证通过返回true
	 */
    public static boolean isEmail(String email)   {
		String regex_email = "[a-z0-9A-Z_.-]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
		return match(regex_email, email);
    }

	/**
	 * 日期验证
	 *
	 * @param date
	 *
	 * @return 验证通过返回true
	 */
	public static boolean isDate(String date) {
		String regex_date = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
		return match(regex_date, date);
	}

	/**
	 * 判断数字是否是整数 负数 小数
	 *
	 * @param number
	 *
	 * @return 验证通过返回true
	 */
	public static boolean isNumber(String number){
		String regex_number = "^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$";
		return match(regex_number, number);
	}

	/**
	 * 有效查询属性值 字符 数字 下划线 点 (防止sql注入)
	 *
	 * @param field
	 *
	 * @return 验证通过返回true
	 */
	public static boolean isField(String field){
		String regex_field = "[a-zA-Z0-9_、.`\"]*";
		return match(regex_field, field);
	}

	/**
	 * 根据指定长度 分隔字符串
	 * 
	 * @param str
	 *            需要处理的字符串
	 * @param length
	 *            分隔长度
	 * 
	 * @return 字符串集合
	 */
	public static List<String> splitString(String str, int length) {
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < str.length(); i += length) {
			int endIndex = i + length;
			if (endIndex <= str.length()) {
				list.add(str.substring(i, i + length));
			} else {
				list.add(str.substring(i, str.length() - 1));
			}
		}
		return list;
	}

	/**
	 * 两个集合进行比较取相同的
	 * 
	 * @param list_pid
	 *            传入集合
     * @param list_id
     *            传入集合
	 * 
	 * @return list 返回一个集合
	 */
	public List<Object> listCompare(List<Object> list_pid, List<Object> list_id) {

		List<Object> list = new ArrayList<Object>();
		
		return list;
	}

	/**
	 * 将字符串List转化为字符串，以分隔符间隔.
	 *
	 * @param strings 字符串集合
	 * @param separator 分隔符
	 *
	 * @return 转化后的字符串
	 */
	public static String toString(Collection<String> strings, String separator) {
		return toString(strings, separator, false);
	}

	/**
	 * 将字符串List转化为字符串，以分隔符间隔.
	 *
	 * @param strings 字符串集合
	 * @param separator 分隔符
	 *
	 * @return 转化后的字符串
	 */
	public static String toString(Collection<String> strings, String separator, boolean around) {
		StringBuffer stringBuffer = new StringBuffer();
		for (String str : strings) {
			stringBuffer.append(separator);
			if (around) stringBuffer.append("'");
			stringBuffer.append(str);
			if (around) stringBuffer.append("'");
		}
		stringBuffer.deleteCharAt(0);
		return stringBuffer.toString();
	}

	/**
	 * 字符串数组转化字符串，以分隔符间隔.
	 * @param strings 字符串数组
	 * @param separator 分隔符
	 *
	 * @return String
	 */
	public static String toString(String[] strings, String separator) {
		return toString(strings, separator, false);
	}

	/**
	 * 字符串数组转化字符串
	 * @param strings 字符串数组
	 * @param separator 分隔符
	 * @param around 单引号
	 *
	 * @return String
	 */
	public static String toString(String[] strings, String separator, boolean around) {
		StringBuffer stringBuffer = new StringBuffer();
		for (String str : strings) {
			stringBuffer.append(separator);
			if (around) stringBuffer.append("'");
			stringBuffer.append(str);
			if (around) stringBuffer.append("'");
		}
		stringBuffer.deleteCharAt(0);
		return stringBuffer.toString();
	}
	
	public static String getAliasPath(String[] str, int index, int begin){
		String result = "";
		if(index < 0){
			return result;
		}
		if(index > str.length){
			index = str.length-1;
		}
		for(int i=begin;i<=index;i++){
			if(i==begin){
				result = str[i];
			}else{
				result = result + "." + str[i];
			}
		}
		return result;
	}
	public static String getAliasPath(String[] str, int index){
		return getAliasPath(str, index, 0);
	}
	
	public static String getPath(String[] str, int index, int begin){
		String result = "";
		if(index < 0){
			return result;
		}
		if(index > str.length){
			index = str.length-1;
		}
		for(int i=begin;i<=index;i++){
			if(i==begin){
				result = str[i];
			}else{
				if(i==index){
					result = result + "." + str[i];
				}else{
					result = result + "_" + str[i];
				}
			}
		}
		return result;
	}
	public static String getPath(String[] str, int index){
		return getPath(str, index, 0);
	}

	public static String getAlias(String[] str, int index, int begin){
		String result = "";
		if(index < 0){
			return result;
		}
		if(index > str.length){
			index = str.length-1;
		}
		for(int i=begin;i<=index;i++){
			if(i==begin){
				result = str[i];
			}else{
				result = result + "_" + str[i];
			}
		}
		return result;
	}
	public static String getAlias(String[] str, int index){
		return getAlias(str, index, 0);
	}

	public static List<String> getReferers(String str){
		String regex_referer = "@([^@^\\s^:]{1,})([\\s\\:\\,\\;]{0,1})";
		Pattern p = Pattern.compile(regex_referer);
		Matcher m = p.matcher(str);
		List<String> referers = new ArrayList<String>();
		while (m.find()) {
			String origion_str = m.group();
			String referer = origion_str.substring(1, origion_str.length()).trim();
			referers.add(referer);
		}
		return referers;
	}

	/**
	 * 去除非法字符
	 *
	 * @param str
	 *
	 * @return String
     */
    public static String cleanString(String str){
//        String regEx="[`~!@#$%^&amp;*()+=|{}':;',\\[\\].&lt;&gt;/?~！@#￥%……&amp;*（）——+|{}【】'；：”“’。，、？]";
		String regEx = "[^a-zA-Z_\\u4e00-\\u9fa5]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
	
	public static void main(String[] args) {
		/*String str = "今天天气好@啦啦 @哈哈 你么你 啦啦";
		List<String> referers = getReferers(str);
//		_GenerateRefererLinks(str, referers);
		for(String s:referers){
			System.out.println(s);
		}
//		String property = "business.client.name";
//		String[] fields = StringUtils.split(property, ".");
//		for (int j = 0; j < fields.length - 1; j++) {
//			System.out.println("Name:"+CommonUtil.getPath(fields, j));
//			System.out.println("Alias:"+fields[j]);
//		}
//		System.out.println("add:"+CommonUtil.getPath(fields, fields.length - 1, fields.length - 2));*/
        //System.out.println(UUID.randomUUID().toString());

//        String str = "\\xF0\\x9F\\x8D\\xB8*adCVs*34_a _09_b5*[/435^*&amp;城池()^$$&amp;*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&amp;999下面是中文的字符￥……{}【】。，；’“'”？";
//        String s = cleanString(str);
//        System.out.println(s);

		String str = "a123";
		System.out.println(isUsername(str));
    }


	public static String getRandomString(int length) {
		if(length <= 0) {
			return "";
		} else {
			char[] randomChar = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'};
			Random random = new Random();
			StringBuffer stringBuffer = new StringBuffer();

			for(int i = 0; i < length; ++i) {
				stringBuffer.append(randomChar[Math.abs(random.nextInt()) % randomChar.length]);
			}

			return stringBuffer.toString();
		}
	}
}
